package com.yc.usc;

import java.io.UnsupportedEncodingException;

public class Tusc {
	/**
	 * 做一个关于SMS的程序，需要对中文部分进行UCS编码，这里用了两个函数，可以将短信中的UCS2字符串在UCS2和GBK之间装换
	 */

	/**
	 * UCS2解码
	 * 
	 * @param src UCS2 源串
	 * @return 解码后的UTF-16BE字符串
	 */
	public static String DecodeUCS2(String src) {
		byte[] bytes = new byte[src.length() / 2];

		for (int i = 0; i < src.length(); i += 2) {
			bytes[i / 2] = (byte) (Integer.parseInt(src.substring(i, i + 2), 16));
		}
		String reValue = null;// 这里为什么一定要初始化
		try {
			reValue = new String(bytes, "UTF-16BE");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return reValue;

	}

	/**
	 * UCS2编码
	 * 
	 * @param src UTF-16BE编码的源串
	 * @return 编码后的UCS2串
	 */
	public static String EncodeUCS2(String src) {

		byte[] bytes = null;// 这里为什么一定要初始化
		try {
			bytes = src.getBytes("UTF-16BE");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		StringBuffer reValue = new StringBuffer();
		StringBuffer tem = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			tem.delete(0, tem.length());
			tem.append(Integer.toHexString(bytes[i] & 0xFF));
			if (tem.length() == 1) {
				tem.insert(0, '0');
			}
			reValue.append(tem);
		}
		return reValue.toString().toUpperCase();
	}
}
